package com.zego.wawaji.ui.rank.order;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.utils.StringUtils;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.rank.bean.Award;

import java.util.List;

/**
 * Created by Su on 2018/1/16.
 */
public class PrizeOrderAdapter extends ExBaseAdapter<Award> {

    public PrizeOrderAdapter(Context context, List<Award> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.prize_order_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.marginBottom.setVisibility(View.GONE);
        holder.marginTop.setVisibility(View.VISIBLE);
        if (position == 0) {
            holder.marginTop.setVisibility(View.GONE);
        }
        if (position == getList().size() - 1) {
            holder.marginBottom.setVisibility(View.VISIBLE);
        }

        final Award info = getItem(position);

        ImageLoader.loadRoundAvatar(getContext(), info.getAwardImg(), holder.order_icon);
        holder.prize_name.setText(info.getAwardName());
        holder.price_desc.setText(info.getAwardRemark());
        holder.prize_time.setText(StringUtils.getBeforeNoFlag(info.getCountTime(), "T"));
        holder.prize_status.setText(isFinish(info.getOrderStatus()) ? "订单已完成" : "订单未完成");
        holder.prize_status.setTextColor(isFinish(info.getOrderStatus()) ?
                getContext().getResources().getColor(R.color.text_gray) : getContext().getResources().getColor(R.color.text_orange));

        holder.btn_check.setVisibility(info.getAwardType() == 2 ? View.GONE : View.VISIBLE);
        holder.btn_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getWawaMgr().reqTransport(info.getTid(), null);
            }
        });

        holder.delete.setVisibility(isFinish(info.getOrderStatus()) ? View.VISIBLE : View.GONE);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getWawaMgr().reqAwardUpdate(info.getId(), -2, null);
            }
        });

        return convertView;
    }

    private boolean isFinish(int order) {
        return order >= 5;
    }

    private class MyViewHolder {
        private View marginTop, marginBottom;
        private ImageView order_icon, delete;
        private TextView prize_name, prize_time, price_desc, prize_status;
        private RelativeLayout btn_check;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            marginTop = convertView.findViewById(R.id.margin_top);
            marginBottom = convertView.findViewById(R.id.margin_bottom);
            order_icon = (ImageView) convertView.findViewById(R.id.order_icon);
            prize_name = (TextView) convertView.findViewById(R.id.prize_name);
            btn_check = (RelativeLayout) convertView.findViewById(R.id.order_logistics);
            prize_time = (TextView) convertView.findViewById(R.id.prize_time);
            price_desc = (TextView) convertView.findViewById(R.id.price_desc);
            delete = (ImageView) convertView.findViewById(R.id.delete);
            prize_status = (TextView) convertView.findViewById(R.id.prize_status);
        }
    }
}
